//
//  HJMyLikeVC.m
//  project
//
//  Created by yefei on 2019/7/24.
//  Copyright © 2019 健健. All rights reserved.
//

#import "HJMyLikeVC.h"
#import "YFDongtaiModel.h"
#import "YFMineCellViewModel.h"
#import "YFMyDongtaiCell.h"
#import "HJOtherDynamic.h"

@interface HJMyLikeVC ()

@property (nonatomic,strong) NSMutableArray <YFMineCellViewModel *>* dataArray;
/**
 点击index
 */
@property (nonatomic,strong) NSIndexPath  * indexpath;
@end

static NSString * const identifier = @"YFMyDongtaiCell";

@implementation HJMyLikeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navTitle = @"我点的赞";
    [self loadNetworkData];
}
-(void)configTableView {
    self.needRefresh = YES;
    self.tableViewStyle = UITableViewStylePlain;
    [super configTableView];
    self.tableView.showsVerticalScrollIndicator = YES;
    self.tableView.emptyView = self.tipEmptyView;
    [self.tableView registerCellByNibName:@"YFMyDongtaiCell"];
    self.indexpath = [NSIndexPath indexPathForRow:0 inSection:0];
}

#warning mark -- 动态
-(NSMutableArray<YFMineCellViewModel *> *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
#pragma mark -- 加载数据
-(void)loadNetworkData {
    NSString * url = [Api stringByAppendingString:myPraisePageApi];
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    params[@"pageCurrent"] = @(self.page);
    [HJHttpTool get:url params:params success:^(HJResponeds *result) {
        YFDongtaiModel * model = [YFDongtaiModel yy_modelWithDictionary:result.data];
        if (self.page==1) {
            [self.dataArray removeAllObjects];
        }
        self.totalPage = [NSNumber numberWithInteger:model.pages];
        NSMutableArray<YFMineCellViewModel *> *VMs = [NSMutableArray arrayWithCapacity:model.records.count];
        for (YFDongtaiRecordModel *recordModel in model.records) {
            YFMineCellViewModel *viewModels = [[YFMineCellViewModel alloc] initWithModel:recordModel];
            if (!viewModels.isPraise) {
                viewModels.isPraise = YES;
            }
            [VMs addObject:viewModels];
        }
        [self.dataArray addObjectsFromArray:VMs];
        [self.tableView endRefresh];
        [self.tableView reloadData];
    } failure:^(id result) {
        [self.tableView endRefresh];
        [self showErrorHUDWithError:result];
    }];
}
#pragma mark - UITableViewDataSource, UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YFMyDongtaiCell * dongtaiCell =[tableView cellForRowAtIndexPath:indexPath];
    if (!dongtaiCell) {
        dongtaiCell = [[NSBundle mainBundle] loadNibNamed:identifier owner:nil options:nil].firstObject;
    }
    dongtaiCell.viewModel = self.dataArray[indexPath.row];
    dongtaiCell.moreButton.hidden = YES;
    dongtaiCell.title = self.navTitle;
    
    dongtaiCell.deleteAction = ^{
        if (self.dataArray.count>0) {
            YFMineCellViewModel *model = self.dataArray[indexPath.row];
            [self.dataArray removeObject:model];
            [tableView reloadData];
        }
    };
    
    return dongtaiCell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.dataArray[indexPath.row].cellHeight;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HJOtherDynamic * dynamicVC = [HJOtherDynamic new];
    dynamicVC.cellModel = self.dataArray[indexPath.row];
    self.indexpath = indexPath;
    dynamicVC.isMine = YES;
    @weakify(self)
    
    dynamicVC.didBackBtnClicked = ^(NSString * _Nonnull zanNum, BOOL isSelecte, BOOL isMine) {
        @strongify(self)
        //        self.dataArray[self.indexpath.row].praiseAmount = zanNum;
        //        self.dataArray[self.indexpath.row].isPraise = isSelecte;
        if (isMine && self.dataArray.count>0) {
            YFMineCellViewModel *model = self.dataArray[self.indexpath.row];
            [self.dataArray removeObject:model];
            [tableView reloadData];
        }
        //        [tableView reloadRowsAtIndexPaths:@[self.indexpath] withRowAnimation:UITableViewRowAnimationNone];
    };
    [self.navigationController pushViewController:dynamicVC animated:YES];
}

@end
